package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.PromotionType;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.handler.DefaultPromotionTagHandler;
import com.ygqh.baby.mapper.YgProductPackDetailMapper;
import com.ygqh.baby.po.YgProduct;
import com.ygqh.baby.po.YgProductPackDetail;
import com.ygqh.baby.po.YgProductPackDetailExample;
import com.ygqh.baby.service.YgProductPackDetailService;
import com.ygqh.baby.utils.LocalArraysUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.*;

@Service
public class YgProductPackDetailServiceImpl implements YgProductPackDetailService {

    @Autowired
    private YgProductPackDetailMapper ygProductPackDetailMapper;

    @Autowired
    private DefaultPromotionTagHandler defailtPromotionTagHandler;

    @Override
    public List<YgProductPackDetail> find() {
        YgProductPackDetailExample example = new YgProductPackDetailExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        return ygProductPackDetailMapper.selectByExample(example);
    }

    @Override
    public int findCount() {
        YgProductPackDetailExample example = new YgProductPackDetailExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        return ygProductPackDetailMapper.countByExample(example);
    }

    @Override
    public YgProductPackDetail findById(Long id) {
        return ygProductPackDetailMapper.selectByPrimaryKey(id);
    }


    @Override
    public int save(YgProductPackDetail ygProductPackDetail) {
        return ygProductPackDetailMapper.insertSelective(ygProductPackDetail);
    }

    @Override
    public int update(YgProductPackDetail ygProductPackDetail) {
        return ygProductPackDetailMapper.updateByPrimaryKeySelective(ygProductPackDetail);
    }

    @Override
    public ResultSet<YgProductPackDetail> search(QueryInfo queryInfo, String q) {
        List<YgProductPackDetail> list = ygProductPackDetailMapper.selectSuper(queryInfo, q);
        int count = ygProductPackDetailMapper.countSuper(q);
        ResultSet<YgProductPackDetail> resultSet = new ResultSet<YgProductPackDetail>(count, list);
        return resultSet;
    }

    @Override
    public List<Long> getProductIdsInOtherPack(Long uncludedPackId, Long[] productIds, Date startDate, Date endDate) {
        return ygProductPackDetailMapper.getProductIdsInOtherPack(uncludedPackId, productIds, startDate, endDate);
    }

    private int addBatch(List<YgProductPackDetail> details) {
        if (CollectionUtils.isEmpty(details)) {
            return 0;
        }
        return ygProductPackDetailMapper.addBatch(details);
    }

    @Override
    public List<Long> findProductIdsByPackId(Long packId) {
        return ygProductPackDetailMapper.selectProductIdsByPackId(packId);
    }

    @Override
    public int addProductPackDetail(Long packId, Long[] productIds, String createBy) {
        Assert.notNull(packId, "packId can't be null.");
        Assert.notNull(productIds, "productIds can't be null.");
        List<YgProductPackDetail> details = new ArrayList<>(productIds.length);
        for (Long productId : productIds) {
            YgProductPackDetail detail = new YgProductPackDetail();
            detail.setPackId(packId);
            detail.setProductId(productId);
            detail.setCreateBy(createBy);
            detail.setUpdateBy(createBy);
            detail.setStatus(DataStatus.Valid.name());
            details.add(detail);
        }
        int i = this.addBatch(details);
        updatePromotionTag(details.get(0).getPackId(), Arrays.asList(productIds), null);
        return i;
    }

    @Override
    public List<YgProduct> findProductByBack(Long packId) {
        return ygProductPackDetailMapper.selectProductByPackId(packId);
    }

    @Override
    public int deleteByDetailId(Long detailId, String deleteBy) {
        YgProductPackDetail detail = ygProductPackDetailMapper.selectByPrimaryKey(detailId);
        detail.setId(detailId);
        detail.setUpdateBy(deleteBy);
        detail.setStatus(DataStatus.Valid.name());
        int i = ygProductPackDetailMapper.updateByPrimaryKeySelective(detail);
        updatePromotionTag(detail.getPackId(), null, Collections.singletonList(detail.getProductId()));
        return i;
    }

    @Override
    public int updateProductPackDetail(Long packId, Long[] productIds, String updateBy) {
        Assert.notNull(packId, "packId can't be null.");
        Assert.notNull(productIds, "productIds can't be null.");
        List<Long> oldProductIdList = ygProductPackDetailMapper.selectProductIdsByPackId(packId);
        List<Long> newProductIdList = LocalArraysUtil.convertToList(productIds);
        LocalArraysUtil.removeRepet(oldProductIdList, newProductIdList);
        if (CollectionUtils.isNotEmpty(newProductIdList)) {
            addProductPackDetail(packId, newProductIdList.toArray(new Long[0]), updateBy);
        }
        if (CollectionUtils.isNotEmpty(oldProductIdList)) {
            deleteBatch(packId, oldProductIdList, updateBy);
        }
        updatePromotionTag(packId, newProductIdList, oldProductIdList);
        return 1;
    }

    private void deleteBatch(Long packId, List<Long> oldProductIdList, String updateBy) {
        ygProductPackDetailMapper.deleteBatch(packId, oldProductIdList, updateBy);
    }

    @Override
    public int saveOrUpdate(YgProductPackDetail ygProductPackDetail) {
        int r;
        if (ygProductPackDetail.getId() != null) {
            r = this.update(ygProductPackDetail);
        } else {
            r = this.save(ygProductPackDetail);
        }
        return r;
    }

    private void updatePromotionTag(Long packId, List<Long> addList, List<Long> deleteList) {
        defailtPromotionTagHandler.updatePromotionTag(packId, PromotionType.Pack, addList, deleteList, true);
    }

}

